/**
 * @author Daniel
 */
var subListArray = [];
		subListArray[0] = 'Select a type first';
		subListArray[1] = ['ca|Cash', 'cr|Credit', 'pp|PayPal'];
		subListArray[2] = ['sh|Shop', 'mt|marketTrader'];

	
//Get all checkboxes
var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;  
var cbs = []; //will contain all checkboxes 
for(var i = 0; i < inputs.length; i++)
{  
  	if(inputs[i].type == "checkbox")
  	{
  		if(inputs[i].value != "termsChkbx")
  		{
    		cbs.push(inputs[i]);
    	}
    }
}

/*
 * Terms and conditions
 */
function termsNcon(item)
{
	if(item.checked == false)
	{
		item.parentNode.style.color			= "#FF0000";
		item.parentNode.style.fontWeight		= "bold";
		document.getElementById('sub1').disabled = true;
	}
	if(item.checked	== true)
	{
		item.parentNode.style.color			= "#000000";
		item.parentNode.style.fontWeight	= "normal";
		document.getElementById('sub1').disabled = false;
	}
}

/*
 * form validation
 */
function checkForm()
{
	//Check what type of customer box we have
	if(document.forms["orderForm"]["customerType"].value == "")
	{
		window.location = window.location + "#customerType";
		return false;
	}
	else if(document.forms["orderForm"]["customerType"].value == "ret")
	{
		//Check customer fields filled in
		if(document.forms["orderForm"]["forename"].value.replace(/^\s*|\s*$/g,'') == "" && document.forms["orderForm"]["surname"].value.replace(/^\s*|\s*$/g,'') == "")
		{
			window.location = window.location + "#customerType";
			alert("You must provide your name");
			return false;
		}
	}
	else if(document.forms["orderForm"]["customerType"].value == "trd")
	{
		//Check company name filled in
		if(document.forms["orderForm"]["companyName"].value.replace(/^\s*|\s*$/g,'') == "")
		{
			window.location = window.location + "#customerType";
			alert("You must provide a company name");
			return false;
		}
	}
	
	//Check checkboxes
	 var found	= false;
	 for(var i = 0; i < cbs.length; i++)
	 {
	 	if(cbs[i].checked == true)
	 	{
	 		found	= true;
	 	}
	 }
	 if(!found)
	 {
	 	window.location = window.location + "#orderForm";
	 	alert("You must select at least one pair of shoes");
	 	return false;
	 }
}

/*
 * Change select box options
 */
function custType()
{
	var form	= document.getElementById('orderForm');
	if(document.forms["orderForm"]["customerType"].value == "")
	{
		document.getElementById('retCustDetails').style.visibility = "hidden";
		document.getElementById('tradeCustDetails').style.visibility = "hidden";
		 
		 //Delete all options
		 document.forms["orderForm"]["subType"].options.length = 0;
		 //set as first array val
		 document.forms["orderForm"]["subType"].options[0] = new Option(subListArray[0]);
	}
	else if(document.forms["orderForm"]["customerType"].value == "ret")
	{
		document.getElementById('retCustDetails').style.visibility = "visible";
		document.getElementById('tradeCustDetails').style.visibility = "hidden";
		
		//Delete all options
		document.forms["orderForm"]["subType"].options.length = 0;
		//Update select box options
		for(var i = 0; i < subListArray[1].length; i++)
		{
			document.forms["orderForm"]["subType"].options[i] = new Option(subListArray[1][i], subListArray[1][i]);
		}
	}
	else if(document.forms["orderForm"]["customerType"].value == "trd")
	{
		document.getElementById('retCustDetails').style.visibility = "hidden";
		document.getElementById('tradeCustDetails').style.visibility = "visible";
		
		//Delete all options
		document.forms["orderForm"]["subType"].options.length = 0;
		//Update select box options
		for(var i = 0; i < subListArray[2].length; i++)
		{
			document.forms["orderForm"]["subType"].options[i] = new Option(subListArray[2][i], subListArray[2][i]);
		}
	}
}

/*
 * checkbox price
 */
 function chkPrice(item)
 {
 	if(item.checked == true)
 	{
 		document.getElementById('total').value = (Number(document.getElementById('total').value) + Number(item.value)).toFixed(2);
 	}
 	else
 	{
 		document.getElementById('total').value = (Number(document.getElementById('total').value) - Number(item.value)).toFixed(2);
 	}
 }

/*
 * delivery price
 */
 function delPrice(item)
 {
 	if(item.title == "3.99")
 	{
 		document.getElementById('total').value = (Number(document.getElementById('total').value) + Number(3.99)).toFixed(2);
 	}
 	else
 	{
 		document.getElementById('total').value = (Number(document.getElementById('total').value) - Number(3.99)).toFixed(2);
 	}
 }
